import * as PIXI from "pixi.js";
// import {PixiApp} from "@/pixi/App";
import {storeToRefs} from "pinia";
import  {usePixiStore}  from '@/store/pixi'
import {spriteContainer} from "@/pixi/spriteContainer";

export class CreateSprite{
    constructor(_params) {
        let {url,name,x,y,width,height,rotateX=0.5,rotateY=0.5} = _params
        const clickSprite = PIXI.Sprite.from(url);
        clickSprite.name = name

        // let pixiApp = PixiApp.init()
        // clickSprite.x = x - pixiApp.app.screen.width / 2;
        // clickSprite.y = y - pixiApp.app.screen.height / 2;

        let store = usePixiStore()
        store.changeCurrentWidth()
        store.changeCurrentHeight()
        let {pixiMaxWidth,pixiMaxHeight,currentWidth,currentHeight } = storeToRefs(store)

        // 不自适应
        // clickSprite.x = x - currentWidth.value / 2 + width/2;
        // clickSprite.y = y - currentHeight.value / 2 + height/2;
        // clickSprite.width = width;
        // clickSprite.height = height;

        // 自适应
        clickSprite.x = x/pixiMaxWidth.value*currentWidth.value - currentWidth.value / 2 + (width/pixiMaxWidth.value*currentWidth.value)/2;
        clickSprite.y = y/pixiMaxHeight.value*currentHeight.value - currentHeight.value / 2 + (height/pixiMaxHeight.value*currentHeight.value)/2;
        clickSprite.width = width/pixiMaxWidth.value*currentWidth.value;
        // clickSprite.height = height/pixiMaxHeight.value*currentHeight.value; // 等比， 在设计稿等比下 显示正常
        clickSprite.height = height * clickSprite.width/width;  // 非等比， 在设计稿等比下 显示正常


        // 设置精灵的锚点
        clickSprite.anchor.set(rotateX, rotateY);
        clickSprite.cursor = 'pointer';
        // clickSprite.interactive = true;
        clickSprite.eventMode = 'static';

        spriteContainer.addChild(clickSprite);

        return clickSprite
    }

    static create(_params){
        return new CreateSprite(_params)
    }
}
